package com.bit.demo.mybatisdemo.mapper;

import com.bit.demo.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select * from user_info")
    List<UserInfo> selectAll();

    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from user_info")
    List<UserInfo> selectAll3();

    @Select("select id,username,password,age,gender,phone," +
            "delete_flag as deleteFlag, create_time as createTime," +
            "update_time as updateTime from user_info")
    List<UserInfo> selectAll2();

    @ResultMap("BaseMap")
    @Select("select * from user_info where id = #{id}")
    UserInfo selectById(Integer id);

    @Select("select * from user_info where username = #{userName} and password = #{password}")
    List<UserInfo> selectByNameAndPassword(String userName,String password);

    @Select("select * from user_info where username = #{param1} and password = #{param2}")
    List<UserInfo> selectByNameAndPassword2(String userName,String password);


    //#{} 用于替换 SQL 语句中的参数，${} 用于拼接 SQL 语句中的参数
    @Select("select * from user_info where username = #{a} and password = #{b}")
    List<UserInfo> selectByNameAndPassword3(@Param("a") String userName, @Param("b") String password);

    @Select("select * from user_info where username =' ${a}' and password =' ${b}'")
    List<UserInfo> selectByNameAndPassword4(@Param("a") String userName, @Param("b") String password);
    //@Options 注解用于配置 SQL 语句的执行选项
    //useGeneratedKeys = true 表示使用数据库的自动生成主键功能
    //keyProperty = "id" 表示将生成的主键值设置到 UserInfo 对象的 id 属性中
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) values(#{username},#{password},#{age})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) " +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer deleteById(Integer id);


    @Update("update user_info set delete_flag=#{deleteFlag},gender=#{gender} where id=#{id}")
    Integer updateById(UserInfo userInfo);


    @Select("select * from user_info order by id ${oder}")
    List<UserInfo> selectNameByOder(String oder);

    @Select("select * from user_info where username like '%${like}%'")
    List<UserInfo> selectByLike(String like);

    @Select("select * from user_info where username like CONCAT('%',#{like},'%')")
    List<UserInfo> selectByLike2(String like);

}
